package Operations;

import java.util.Map;
import java.lang.reflect.Field;
import java.util.List;

import Operations.Operation;
import GA.ConstructorDeclared;
import util.Filter;

public class AssignConstant extends Operation{
	private final Class<?> clz;
	private final String ref;
	private final String value;
	public AssignConstant(Class<?> clz,String ref, String value) {
		this.clz=clz;
		this.ref = ref;
		this.value = value;
	}
	

	public  Class<?>  getClazz(){
		return this.clz;		
	}
	public  String  getRefrence(){
		return this.ref;		
	}
	public  String  getValue(){
		return this.value;		
	}

	public static AssignConstant generate(Class<?> clz,String cName, ConstructorDeclared cutConst,Map<String, String[]>  refFactory) throws ClassNotFoundException{
		if(Filter.isObjectType(clz)) return null;
		Class<?> javaClass= Class.forName(clz.getName());
		List<Field> constants = cutConst.getConstantsList();
		if(constants.size()==0) return null;
		String[] refrences=refFactory.get(cName);
		int randRef=ValuesGenerator.getRandomInt(refrences.length);
		String ref= refrences[randRef];
		String value=null;
		if(constants.size() > 0 ) {
			//&& !ValuesGenerator.getBoleean(Operation.SET_TO_NULL)
			int randField=ValuesGenerator.getRandomInt(constants.size());
			Field field= constants.get(randField);
			value=field.getName();
			return new AssignConstant(clz,ref,value);
			
		}
		return null;
	}
	@Override
	public Operation clone() {
		final AssignConstant clone = new AssignConstant(clz,ref, value);

		return clone;
	}
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		
		sb.append(ref+ " = ");
		if(value == null)  sb.append("null".toString() );
		else	sb.append(clz.getName()+"." + value);
		
		return sb.toString();

	}


}
